package com.springboot.cropsboot.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.cropsboot.entity.Admin;
import com.springboot.cropsboot.qpage.QPage;
import com.springboot.cropsboot.service.IAdminService;
import com.springboot.cropsboot.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-12-02
 */
@Validated
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private IAdminService iAdminService;


    @PostMapping("/adLogin")
    public ResultView adLogin(@NotNull(message = "账号不能为空") String account,
                              @NotNull(message = "密码不能为空") String pwd){
        //进行登录认证
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("pwd",pwd);
        Admin admin=this.iAdminService.getOne(queryWrapper);
        if(admin==null){
            return ResultView.error("账号密码错误！！！");
        }
        //进行授权注入sa-token权限框架
        StpUtil.login(admin.getId());
        Map<String,Object> map=new HashMap<>();
        map.put("token",StpUtil.getTokenValue());
        map.put("accountType",admin.getType());
        return ResultView.ok(map);
    }

    /**
     * 列表分页
     * @param obj
     * @return
     */
    @GetMapping("/getPageList")
    public ResultView getPageList(QPage qPage, Admin obj) {
        return this.iAdminService.getPageList(qPage,obj);
    }

    @PostMapping("/register")
    public ResultView adLogin(@NotNull(message = "账号不能为空") String account,
                              @NotNull(message = "密码不能为空") String pwd,
                              @NotNull(message = "名称不能为空") String name){
        //进行登录认证
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        Admin admin=this.iAdminService.getOne(queryWrapper);
        if(admin!=null){
            return ResultView.error("账号已存在！！！");
        }
        admin=new Admin();
        admin.setAccount(account);
        admin.setPwd(pwd);
        admin.setType(2);
        admin.setName(name);
        boolean count=this.iAdminService.save(admin);
        if(!count){
            return ResultView.error("数据异常，注册失败！！！");
        }
        return ResultView.ok();
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping(value = "/getAccountInfo")
    public ResultView getAccountInfo() {
        String managerId= StpUtil.getLoginIdAsString();
        Admin sysManager=this.iAdminService.getById(managerId);
        return ResultView.ok(sysManager);
    }



}
